<template>
    <div class="login-container">
        <div class="login-content">
            <!-- <img src="@/assets/logo2.png" alt="辅助图片" class="login-image"> -->
            <div style="text-align: center;">
                
                <img src="http://cdn.qiniu.liyansheng.top/img/20240725093456.png" alt="">
            </div>
            <el-card class="login-card" shadow="hover">
                <div slot="header" class="login-title">
                    <h3>云农业信息共享</h3>
                </div>
                <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="0" class="login-form">
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" aria-required="true" placeholder="用户名"
                            prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" aria-required="true" type="password" placeholder="密码"
                            prefix-icon="el-icon-lock"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" class="login-button" @click="submitForm">登录</el-button>
                        <a href="/register">去注册</a>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script>
export default {
    name: 'LoginPage',
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm() {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    // 表单验证通过，执行登录逻辑
                    this.login();
                } else {
                    // 表单验证不通过，可以做一些提示或者其他操作
                }
            });
        },
        login() {
            // 模拟登录成功
            console.log('Username:', this.loginForm.username);
            console.log('Password:', this.loginForm.password);

            this.$axios.post('/login', this.loginForm)
                .then(res => {
                    console.log(res.data)
                    if (res.code === 200) {
                        localStorage.setItem('token', res.data.token.tokenValue);
                        localStorage.setItem('role', res.data.user.role)
                        this.$store.dispatch('login', res.data.user); // 调用 Vuex action 保存用户信息
                        // 处理登录成功的逻辑，例如跳转到首页
                        this.$message.success('登陆成功');
                        this.$router.push('/index');
                    } else {
                        this.$message.error(res.msg);
                    }
                })
                .catch(error => {
                    console.error('Login failed:'+error);
                    // 处理登录失败的逻辑
                });
        }
    }
};
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 80vh;
    /* background-color: #f5f5f5; */
    /* 设置背景颜色 */
}

.login-content {
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-card {
    width: 400px;
    max-width: 80%;
    text-align: center;
    padding: 30px;
    background-color: #d9d9d9;
}

.login-image {
    width: 400px;
    /* 确保图片和登录卡片大小一致 */
    height: auto;
    /* 根据图片比例调整高度 */
    margin: 0 20px;
    /* 添加适当的间距 */
}

.login-title {
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 20px;
}

.login-form {
    text-align: left;
}

.login-button {
    width: 100%;
}
</style>
